package com.nkym.controller;/*
 * @ClassName InterfaceInfoController
 * @Desc TODO
 * @Author 19637
 * @Date 2023/4/18 21:22
 * @Version 1.0
 */

import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.nkym.annotations.AuthCheck;
import com.nkym.common.BaseResponse;
import com.nkym.common.ErrorCode;
import com.nkym.exception.BusinessException;
import com.nkym.pojo.dto.interfaceInfo.InterfaceInfoInvokeRequest;
import com.nkym.pojo.dto.interfaceInfo.InterfaceInfoUpdateRequest;
import com.nkym.utils.ResultUtil;
import com.nkym.pojo.dto.IdRequest;
import com.nkym.pojo.dto.interfaceInfo.InterfaceInfoAddRequest;
import com.nkym.pojo.dto.interfaceInfo.InterfaceInfoQueryRequest;
import com.nkym.pojo.entity.InterfaceInfo;
import com.nkym.pojo.vo.PageVo;
import com.nkym.service.InterfaceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/interfaceinfo")
@Api(tags = "interfaceinfoAdmin")
public class InterfaceInfoController {

    @Resource
    InterfaceInfoService interfaceInfoService;

    @GetMapping("/id")
    @ApiOperation("根据ID查询接口信息")
    public BaseResponse<InterfaceInfo> getInterfaceInfoById(IdRequest idRequest) {
        if (idRequest == null || idRequest.getId() == null)
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        InterfaceInfo interfaceInfo = interfaceInfoService.getById(idRequest.getId());
        return ResultUtil.success(interfaceInfo);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除接口信息")
    @AuthCheck("admin")
    public BaseResponse<InterfaceInfo> deleteInterfaceInfoById(@PathVariable Integer id, HttpServletRequest request) {
        boolean remove = interfaceInfoService.removeById(id);
        if (remove) {
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    @PostMapping("/add")
    @ApiOperation("新增接口信息")
    @AuthCheck("admin")
    public BaseResponse<InterfaceInfo> addInterfaceInfo(@RequestBody InterfaceInfoAddRequest infoAddRequest, HttpServletRequest request) {
        interfaceInfoService.addInterfaceInfo(infoAddRequest,request);
        return ResultUtil.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改接口信息")
    @AuthCheck("admin")
    public BaseResponse<InterfaceInfo> updateInterfaceInfo(@RequestBody InterfaceInfoUpdateRequest infoUpdateRequest, HttpServletRequest request) {
        interfaceInfoService.updateInterfaceInfo(infoUpdateRequest);
        return ResultUtil.success("修改成功");
    }

    @GetMapping("/query")
    @ApiOperation("分页查询接口信息")
    public BaseResponse<PageVo<InterfaceInfo>> paginationQuery(InterfaceInfoQueryRequest queryRequest) {
        PageVo<InterfaceInfo> paginationQuery = interfaceInfoService.paginationQuery(queryRequest);
        return ResultUtil.success(paginationQuery);
    }

    @AuthCheck("admin")
    @PutMapping("/online")
    @ApiOperation("接口上线")
    public BaseResponse<InterfaceInfo> online(@RequestBody IdRequest idRequest, HttpServletRequest request) {
        interfaceInfoService.online(idRequest);
        return ResultUtil.success();
    }

    @AuthCheck("admin")
    @PutMapping("/offline")
    @ApiOperation("接口下线")
    public BaseResponse<InterfaceInfo> offline(@RequestBody IdRequest idRequest, HttpServletRequest request) {
        interfaceInfoService.offline(idRequest);
        return ResultUtil.success();
    }

    @PostMapping("/invoke")
    @ApiOperation("模拟调用接口")
    public BaseResponse<String> invoke(@RequestBody InterfaceInfoInvokeRequest invokeRequest, HttpServletRequest request) {
        String invoke = interfaceInfoService.invoke(invokeRequest, request);
        return ResultUtil.success(invoke, "成功");
    }
}
